﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BusinessObjects.BO_GetObjects;
using BussinessObjects;
using BusinessObjects;
using LibraryO2S;
using BusinessObjects.BO_Actions.Services;
using System.Collections.Generic;
using Entity.StoreProcedure;

public partial class controls_admin_Services : Pages_Base
{
    protected int m_id;
    private DataTable objDT;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["m_id"] != null)
            m_id = Convert.ToInt32(Request["m_id"]);
        else m_id = -1;
        base.removeHGC(divMessage);
        base.removeHGC(divResult);
        if (!IsPostBack)
        {
            base.loadCategories(ddlCategory, 5);
            if (Request["c_id"] != null)
                ddlCategory.SelectedValue = Request["c_id"].ToString();
            loadData();
        }
    }
    protected void loadData()
    {
        try
        {
            string keyword = String.Empty;
            int c_id = -1;
            if (Request["k_w"] != null)
            {
                keyword = Request["k_w"].ToString();
                if (m_id == -1)
                    c_id = Convert.ToInt32(ddlCategory.SelectedValue);
                update.Visible = false;
            }
            else
            {
                if (m_id != -1)
                {
                    DataTable module = BO_Module.Search(m_id);
                    string type = module.Rows[0]["TYPE_ID"].ToString();
                    if (!type.Equals("4"))
                    {
                        c_id = Convert.ToInt32(ddlCategory.SelectedValue);
                        update.Visible = false;
                    }
                }
                else
                {
                    c_id = Convert.ToInt32(ddlCategory.SelectedValue);
                    update.Visible = false;
                }
            }
            objDT = BO_Services.SelectAdmin(keyword, m_id, c_id, base.Cur_page, base.Page_size);
            bindRepeater(rptData, objDT, pagerContainer, divMessage);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    protected void btnDel_Click(object sender, EventArgs e)
    {
        string del_list = hdfID.Value;
        string[] sp_list = del_list.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
        BOServices_Delete _action = new BOServices_Delete();
        usp_Services_Delete _entity;
        List<usp_Services_Delete> _listData = new List<usp_Services_Delete>();
        foreach (string per in sp_list)
        {
            _entity = new usp_Services_Delete();
            _entity.SERVICE_ID = Convert.ToInt32(per);
            _listData.Add(_entity);
        }
        if (_action.ExecuteListDataAndReturnBooleanValue(_listData))
            divMessage.InnerHtml = O2S_Message.Success("Thông Báo", "Thực hiện thành công.");
        else
            divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Chưa thực hiện được !");
        loadData();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string keyword = txtSearch.Value.Trim();
        if (keyword != null && !keyword.Equals("") && !keyword.Equals("Hãy nhập từ khóa !"))
        {
            string url = base.removeRequest();
            Response.Redirect(url + "&k_w=" + keyword);
        }
    }
    //protected void rptData_ItemCommand(object source, RepeaterCommandEventArgs e)
    //{
    //    try
    //    {
    //        base.OpenConnection();
    //        int e_id = Convert.ToInt32(e.CommandArgument);
    //        if (e.CommandName.Equals("dialog"))
    //        {
    //            DataRow row = BO_Members.Search(e_id).Rows[0];
    //            hdfDialog.Value = "1";
    //            ltrAddress.Text = row["ADDRESS"].ToString();
    //            ltrEmail.Text = row["EMAIL"].ToString();
    //            ltrFax.Text = row["FAX"].ToString();
    //            ltrFullName.Text = row["FULL_NAME"].ToString();
    //            ltrMemberCode.Text = row["MEMBER_CODE"].ToString();
    //            ltrMonney.Text = row["LEFT_MONEY"].ToString();
    //            ltrPhone.Text = row["MOBILE"].ToString();
    //            ltrUserName.Text = row["USER_NAME"].ToString();
    //        }
    //    }
    //    catch (Exception ex)
    //    {
    //        divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Phát hiện lỗi: <br />" + ex.Message);
    //    }
    //    finally { base.CloseConnection(); }
    //}
    protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
    {
        string url = base.removeRequest();
        if (Request["c_id"] != null)
        {
            int conti = url.LastIndexOf("c_id=");
            string start = url.Substring(0, url.LastIndexOf("&c_id="));
            string end = url.Substring(conti, url.Length - conti);
            conti = end.IndexOf("&");
            if (conti > -1)
                url = start + end.Substring(conti, end.Length - conti);
            else
                url = start;
        }
        Response.Redirect(url + "&c_id=" + ddlCategory.SelectedValue);
    }
}
